package com.linkage.lcims.base.service.broadband.impl;

import java.util.HashMap;
import java.util.Map;

import com.linkage.lcims.base.dao.broadband.interfaces.BroadbandUserDAO;
import com.linkage.lcims.base.po.broadband.BBroadbandUser;
import com.linkage.lcims.base.po.customer.MCustomer;
import com.linkage.lcims.base.service.broadband.interfaces.BroadbandUserBaseBO;
import com.linkage.lcims.base.service.customer.interfaces.CustomerBaseBO;
import com.linkage.lcims.base.service.system.interfaces.AdminLogBO;
import com.linkage.lcims.base.AdminBean;
import com.linkage.util.exception.ServiceException;

public abstract class BroadbandUserBaseBOImpl implements BroadbandUserBaseBO{
	private String message;

	protected BroadbandUserDAO broadbandUserDAO;

	protected AdminLogBO adminLogBO;

	protected CustomerBaseBO customerBO;
	

	protected abstract boolean checkForCommon(BBroadbandUser tmpUser,
			AdminBean admin) throws ServiceException;

	
	public void insertSubmit(BBroadbandUser user,AdminBean admin)
			throws ServiceException {
		try {
			MCustomer customer = new MCustomer(user.getComp_id().getUsername());
			customerBO.updateCustomerServicelist(customer, user.getComp_id()
					.getServicetype().intValue(),admin);
			broadbandUserDAO.saveBroadbandUser(user);
		} catch (Exception e) {
			throw new ServiceException(e, this.getClass());
		}
	}

	public void deleteSubmit(BBroadbandUser user,AdminBean admin)
			throws ServiceException {
		try {
			customerBO.removeCustomerServicelist(new MCustomer(
					user.getComp_id().getUsername()),
					user.getComp_id().getServicetype().intValue(),admin);
			broadbandUserDAO.deleteBroadbandUser(user);
		} catch (Exception e) {
			throw new ServiceException(e, this.getClass());
		}
		
	}

	public void updateSubmit(BBroadbandUser user,AdminBean admin)
			throws ServiceException {
		try {
			broadbandUserDAO.updateBroadbandUser(user);
		}catch (Exception e) {
			throw new ServiceException(e, this.getClass());
		}
	}

	public BBroadbandUser getBroadbandUser(BBroadbandUser tmpUser)
			throws ServiceException {
		try {
			BBroadbandUser user = broadbandUserDAO.getBroadbandUser(
					tmpUser.getComp_id().getUsername(), tmpUser.getComp_id()
							.getServicetype().intValue());
			return user;
		} catch (Exception e) {
			throw new ServiceException(e, this.getClass());
		}
	}
	
	
	
	public MCustomer getCustomer(BBroadbandUser user)
			throws ServiceException {
		try {
			String customerName = user.getComp_id().getUsername();
			MCustomer customer = new MCustomer(customerName);
			customer = customerBO.getCustomer(customer);
			return customer;
		} catch (Exception e) {
			throw new ServiceException(e, this.getClass());
		}
		
	}

	protected boolean isBroadbandUserExist(String userName, int serviceType)
			throws ServiceException {
		try {
			if (broadbandUserDAO.getBroadbandUser(userName,
					serviceType) == null)
				return false;
			else
				return true;
		} catch (Exception e) {
			throw new ServiceException(e, this.getClass());
		}
	}


	@SuppressWarnings("unchecked")
	public void updateFieldsSubmit(BBroadbandUser user,
			Map<String, Object> updateFields) throws ServiceException {
		
		Map key=new HashMap();
		key.put("username", user.getComp_id().getUsername());
		key.put("servicetype", user.getComp_id().getServicetype());
		try {
			broadbandUserDAO.updateFields(key, updateFields);
		} catch (Exception e) {
			throw new ServiceException(e);
		}
	}


	public BroadbandUserDAO getBroadbandUserDAO() {
		return broadbandUserDAO;
	}


	public void setBroadbandUserDAO(BroadbandUserDAO broadbandUserDAO) {
		this.broadbandUserDAO = broadbandUserDAO;
	}


	public AdminLogBO getAdminLogBO() {
		return adminLogBO;
	}


	public void setAdminLogBO(AdminLogBO adminLogBO) {
		this.adminLogBO = adminLogBO;
	}


	public CustomerBaseBO getCustomerBO() {
		return customerBO;
	}


	public void setCustomerBO(CustomerBaseBO customerBO) {
		this.customerBO = customerBO;
	}


	public String getMessage() {
		return message;
	}


	public void setMessage(String message) {
		this.message = message;
	}

}
